﻿using Esint.JudicialAuction.BLL;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Threading;
using System.Web;
using WebJudicialAuction.admin.bases;
using Esint.Common;

namespace WebJudicialAuction.admin.handler
{
    /// <summary>
    /// login 的摘要说明
    /// </summary>
    public class login : BaseHandler, IHttpHandler
    {
        public login()
        {
            IgnoreUserLogin = true;
        }

        Sys_UserService userBll = new Sys_UserService();
        Sys_UserPowerService powerBll = new Sys_UserPowerService();

        protected override AjaxResult BusinessHandleFunc(string req_type, Dictionary<string, string> dicData)
        {
            AjaxResult ajaxResult = new AjaxResult();

            switch (req_type)
            {
                default:
                    break;

                case "permissionToLogin":
                    {
                        //Thread.Sleep(3000);

                        var userModel = userBll.GetUserModelByUserNameAndPwd(dicData["uname"], dicData["userpwd"]);

                        if (userModel == null)
                        {
                            ajaxResult.flag = AjaxResultFlag.Failed;
                            ajaxResult.msg = AjaxMessage.LoginFailed;
                        }
                        else
                        {
                            this.OwnContext.Session[SessionKey.UserInfo] = userModel;

                            var powerList = powerBll.GetUserPowerListByUserId(userModel.UserId);
                            if (powerList.IsNotNullOrCountZero())
                            {
                                userModel.UserPowerList.AddRange(powerList);
                            }

                            ajaxResult.flag = AjaxResultFlag.Success;
                            ajaxResult.msg = AjaxMessage.LoginSuccess;
                        }

                        break;
                    }
            }

            return ajaxResult;
        }
    }
}